<template>
	<view class="edit-page">
		<van-address-edit :area-list="areaList" show-delete show-set-default show-search-result
			:search-result="searchResult" :area-columns-placeholder="['请选择', '请选择', '请选择']" :address-info="AddressInfo"
			@save="onSave" @delete="onDelete" @change-detail="onChangeDetail" />
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		showToast
	} from 'vant';
	import {
		areaList
	} from '@vant/area-data';
	import {
		useStore
	} from 'vuex'
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import api from './../../lib/request.js'
	let list = ref(['请选择', '请选择', '请选择'])
	// 搜索结果
	const searchResult = ref([]);
	const store = useStore()
	let id = ref(store.state.uid)
	console.log(id.value);
	let aid = ref('')
	let AddressInfo = ref({})
	// 保存
	const onSave = (val) => {
		console.log(val)
		const data = {
			userId: id.value,
			name: val.name,
			province: val.province,
			city: val.city,
			area: val.areaCode,
			detailedAddress: val.addressDetail,
			iphone: val.tel
		};

		api.post('/address/add', data).then(res => {
			console.log(res);
			if (res.code) {
				uni.showToast({
					title: '已新增地址',
					icon: 'none',
				})
			}
		})
	}

	//id
	onLoad(({
		id
	}) => {
		// aid.value = id
		uni.setNavigationBarTitle({
			title: id ? '编辑地址' : '新增地址'
		})
		//编辑
		if (id) {
			api.get(`/address/findAllById/${id}`).then(res => {
				console.log(res);
				area: "140303"
				let {
					data:re
				} = res
				let addr = {
					addressDetail:re.detailedAddress,
					areaCode: re.area
					// province: re.province
					// tel: re.iphone
					
				
				}
				
				console.log(addr);
				
				console.log(data.area);
				let {
					city_list,
					county_list
				} = areaList
				let ci;
				// 得到市对应的编码
				for (var k in city_list) {
					if (city_list[k] == data.city) {
						ci = k.slice(0, 4)
					}
				}
				for (var k in county_list) {
					if (k.includes(ci) && county_list[k] == data.area) {
						obj.areaCode = k
					}
				}
				console.log(ci);
			})
		}
	})
	// 删除
	const onDelete = () => showToast('delete');
	// 修改详细地址时触发	
	const onChangeDetail = (val) => {
		console.log(val);
	}
</script>

<style>

</style>